<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">

    <j-form-container>
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="下单时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="placeOrderTime">
              <j-date placeholder="请选择下单时间" v-model="model.placeOrderTime" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="送达时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deliveredTime">
              <j-date placeholder="请选择送达时间" v-model="model.deliveredTime" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </j-modal>
</template>

<script>

  import {postAction} from '@/api/manage'

  export default {
    name: 'placeOrderModal',
    data () {
      return {
        title:'',
        width:800,
        visible: false,
        disableSubmit: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {
          placeOrderTime: [{ required: true, message: '下单时间不能为空' }],
          deliveredTime: [{ required: true, message: '送达时间不能为空' }],
        },
        url: {
          placeOrderAdd: "/ghProjectPlaceOrder/add",
        },
        chooseCostList:[],
        customerId: 0,
      }
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      close () {
        this.visible = false;
        this.model = {};
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            that.model.customerId = this.customerId;
            that.model.chooseCostVOList = this.chooseCostList;
            postAction(this.url.placeOrderAdd, that.model).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.close();
              that.confirmLoading = false;
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      add(chooseCostList,customerId) {
        this.title = "下单";
        this.customerId = customerId;
        this.chooseCostList = chooseCostList;
        this.visible = true;
      },
    }
  }
</script>